package westh.ilib.service;

import net.sf.json.*;

import org.apache.commons.logging.*;
import org.mule.extras.client.MuleClient;
import org.mule.impl.MuleMessage;
import org.mule.umo.UMOMessage;

import westh.config.Config;

public class RecService 
{

	Log log ;
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");
	public String getUserList()
	{
		JSONObject inJson = new JSONObject();
		inJson.put("methodName", "getCustomerList");
		JSONObject inStr = new JSONObject();
		inStr.put("name1", "name1");
		inJson.put("jsonStr", inStr);
		String strUserList = "";
		try {
			MuleClient clientD = new MuleClient();
			//调用WebService
			log.debug("3");
			UMOMessage umo = clientD.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=rulesControl",inJson.toString(),null);
//			UMOMessage umo = clientD.send("axis:http://192.168.18.200:4381/LIBJson/iDlibJson?method=getCustomerList",inStr.toString(),null);
			
			log.debug("4");
			strUserList = (String)umo.getPayload();
			log.debug("userList=" + strUserList);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return strUserList;
	}	
	
	public String getRecResult(String strUserList)
	{
		log.debug(strUserList);
		String strRecResult = "";
		try {
			MuleClient clientD = new MuleClient();
			//调用WebService tagsys
			log.debug("1");
			UMOMessage umo = clientD.send("axis:http://"+remoteUrl+":4481/DataCenter/TagDataCenter?method=getRecommend",strUserList,null);
			log.debug("2");
			strRecResult = (String)umo.getPayload();
			log.debug("recResult=" + strRecResult);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return strRecResult;
	}

	public RecService(Log log)
	{
		this.log = log;
		String strUserList = getUserList();
		log.debug("6");
		String recResult = getRecResult(strUserList);
		log.debug("7: " + recResult);
		JSONObject jsonRecResult = JSONObject.fromObject(recResult);
		String retCode_rr = jsonRecResult.getString("retCode");
		if (retCode_rr.equals("3031"))
		{
			//userId, result:[{rscId, rank}]
			JSONArray recResultArray = JSONArray.fromObject(jsonRecResult.getString("result"));
			
			log.debug("recResultArray: " + recResultArray);
			JSONArray jsonRecArray = new JSONArray();
			
			for (int j = 0; j < recResultArray.size(); j++)
			{
				JSONObject jsonUserResult = JSONObject.fromObject(recResultArray.get(j));
				
				String userId = jsonUserResult.getString("userId");
				JSONArray recArray = JSONArray.fromObject(jsonUserResult.getString("result"));
				if (recArray == null)
					continue;
				int recArraySize = recArray.size();
				if (recArraySize == 0)
					continue;
				log.debug("recArraySize: " + recArraySize);
				for (int i = 0; i < recArraySize; i++)
				{
					JSONObject jsonTemp= JSONObject.fromObject(recArray.get(i));
					String rscId = jsonTemp.getString("rscId");
					String listRank = jsonTemp.getString("listRank");
					log.debug("rscId: " + rscId);
					log.debug("listRank: " + listRank);
					//get rscInfo though rscID
					
					JSONObject inJson1 = new JSONObject();
					inJson1.put("methodName", "getRscInfoById");
					JSONObject inStr1 = new JSONObject();
					inStr1.put("rscId", rscId);
					inJson1.put("jsonStr", inStr1);				
					
					log.debug("toGuojun: " + inStr1.toString());
					String rscInfo = "";
					try {
						MuleClient client = new MuleClient();
						//调用WebService
						UMOMessage umo = client.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=getRscInfoById",inStr1.toString(),null);
						rscInfo = (String)umo.getPayload();
						log.debug("rscInfo: " + rscInfo);
					} catch (Exception e) {
						throw new RuntimeException(e);
					}	
					

					JSONObject jsonRscInfo = JSONObject.fromObject(rscInfo);
					String retCode_ri = jsonRscInfo.getString("retCode");
					if (retCode_ri.equals("0"))
					{
						JSONObject jsonRscResult = jsonRscInfo.getJSONObject("result");
						String rscTitle = "";
						if (jsonRscResult.containsKey("rscTitle"))
							rscTitle = jsonRscResult.getString("rscTitle");
						String rscAuthor = "";
						if (jsonRscResult.containsKey("rscFirstAuthor"))
							rscAuthor = jsonRscResult.getString("rscFirstAuthor");
						
						JSONObject jsonResult = new JSONObject();
						jsonResult.put("userId", userId);
						jsonResult.put("rscId", rscId);
						if (rscTitle == null || rscTitle == "")
							jsonResult.put("rscTitle", "");
						else
							jsonResult.put("rscTitle", rscTitle);
						if (rscAuthor == null || rscAuthor == "" )
							jsonResult.put("rscAuthor", "");
						else
							jsonResult.put("rscAuthor", rscAuthor);
						jsonResult.put("listRank", i);
						
						log.debug("jsonResult: " + jsonResult.toString());
						
						jsonRecArray.add(jsonResult);
					}
/*
					JSONObject jsonRscInfo = JSONObject.fromObject(rscInfo);
					String retCode_ri = jsonRscInfo.getString("retCode");
					if (retCode_ri.equals("0"))
					{
						JSONObject jsonRscResult = jsonRscInfo.getJSONObject("result");
						String rscTitle = jsonRscResult.getString("rscTitle");
						String rscAuthor = jsonRscResult.getString("rscAuthor");
						
						//String rscTitle = "awefaw";
						//String rscAuthor = "asdf";
						
						JSONObject jsonResult = new JSONObject();
						jsonResult.put("userId", userId);
						jsonResult.put("rscId", rscId);
						jsonResult.put("rscTitle", rscTitle);
						jsonResult.put("rscAuthor", rscAuthor);
						jsonResult.put("listRank", i);
						
						log.debug("jsonResult: " + jsonResult.toString());
						
						jsonRecArray.add(jsonResult);
					}
*/
				}
					
				JSONObject inJson2 = new JSONObject();
				inJson2.put("methodName", "addRecResult");
				inJson2.put("jsonStr", jsonRecArray);		
					
				log.debug(inJson2);
				String strRecSave = "";
				try {
					MuleClient client = new MuleClient();
					//调用WebService
					UMOMessage umo = client.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=addRecResult",jsonRecArray.toString(),null);
					strRecSave = (String)umo.getPayload();
					log.debug("strRecSave: " + strRecSave);
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		}
	}
}
